package com.yejhxt.api;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.yejhxt.inter.deviceMapper;
import com.yejhxt.inter.messageMapper;
import com.yejhxt.inter.roleMapper;
import com.yejhxt.inter.sosMapper;
import com.yejhxt.inter.userMapper;
import com.yejhxt.model.device;
import com.yejhxt.model.role;
import com.yejhxt.model.sos;
import com.yejhxt.model.user;
import com.yejhxt.tools.ProjectTools;

@Controller
@RequestMapping(value = "api/sos/", method = RequestMethod.POST)
public class sosmanager {
	@Resource
	private userMapper userMapper;
	@Resource
	private roleMapper roleMapper;
	@Resource
	private deviceMapper deviceMapper;
	@Resource
	private messageMapper messageMapper;
	@Resource
	private sosMapper sosMapper;
	@RequestMapping(value = "getonesos.do", method = RequestMethod.POST)
	
	public void getone(@RequestParam(value = "access_token", defaultValue = "") String sid,
			@RequestParam(value = "status", defaultValue = "") String state,
			HttpServletRequest request,
			Model model, HttpSession session, HttpServletResponse response) {
		int status = 403;
		long nums = 0;
		List<sos> sos = null;
		user user = null;
		if (session != null) {
			user = (user) session.getAttribute("User");
			if (user == null)
			{
				session = ProjectTools.getAppSession(sid);
				if(session!=null)
				user = (user) session.getAttribute("User");
			}
		}
		
	
		
		if (user == null)
			status = 403;

		else {
			Map<String, Object> map1 = new HashMap<String, Object>();
			if(StringUtils.isEmpty(state))
			map1.put("status", 1);
			else
				map1.put("status", state);
			map1.put("roleid", user.getUserid());
		System.out.println("sos信息查询 state="+state+"   用户"+user.getUserid());
			if (sosMapper.selectByCondition(map1) != 0) {
				/*
				map1.clear();*/
				nums=1;
				sos = sosMapper.selectlast(map1);
			}
			System.out.println("sos信息搜索结束 结果   userid="+user.getUserid()+" nums="+nums);
			status = 200;
		}

		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("status", "success");
			map.put("result", nums);
			map.put("detail", sos);
			map.put("code", 200);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {

			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "parameters error");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}

	
	
	
	
	
	
	
	
	
	
	
@RequestMapping(value = "getsosofrole.do", method = RequestMethod.POST)
	
	public void getSOSofRole(@RequestParam(value = "access_token", defaultValue = "") String sid,
			HttpServletRequest request,
			Model model, HttpSession session, HttpServletResponse response) {
		int status = 403;
		long nums = 0;
		List<sos> sos = null;
		user user = null;
		if (session != null) {
			user = (user) session.getAttribute("User");
			if (user == null)
			{
				session = ProjectTools.getAppSession(sid);
				if(session!=null)
				user = (user) session.getAttribute("User");
			}
		}
		
	
		
		if (user == null)
			status = 403;

		else {
			Map<String, Object> map1 = new HashMap<String, Object>();
			
			map1.put("roleid", user.getUserid());
				sos = sosMapper.selectSOSofRole(map1);
			System.out.println("sos信息搜索结束 结果   userid="+user.getUserid()+" nums="+nums);
			
			if(sos!=null)
				nums=sos.size();
			status = 200;
			
			
		}

		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("status", "success");
			map.put("nums", nums);
			map.put("result", sos);
			map.put("code", 200);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {

			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "parameters error");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	
	
	
	
	
	
	
	
	
	
	@RequestMapping(value = "changesos.do")
	public void getonedetail(@RequestParam(value = "access_token", defaultValue = "") String sid,
			@RequestParam(value = "sosiid", defaultValue = "") String sosiid,
			HttpServletRequest request, Model model, HttpSession session, HttpServletResponse response) {
		int status = 403;
		role role = null;
		user user = null;
		if (session != null) {
			user = (user) session.getAttribute("User");
			if (user == null)
				session = ProjectTools.getAppSession(sid);
		}
		if (user == null)
			status = 403;
		else {

			sos s=new sos();
			s.setSosiid(new Long(sosiid));
			s.setStatus(2);
			sosMapper.updateByPrimaryKeySelective(s);

			status = 200;
		}

		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("status", "success");
			map.put("code", 200);
			map.put("result", role);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {

			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "parameters error");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	@RequestMapping(value = "getsosdetail.do")
	public void getsosdetail(@RequestParam(value = "access_token", defaultValue = "") String sid,
			@RequestParam(value = "sosiid", defaultValue = "") String sosiid,
			HttpServletRequest request, Model model, HttpSession session, HttpServletResponse response) {
		int status = 403;
		role role = null;
		user user = null;
		sos sos = null; 
		if (session != null) {
			
			user = (user) session.getAttribute("User");
			role = (role) session.getAttribute("Role");
			if (user == null)
				session = ProjectTools.getAppSession(sid);
			session = ProjectTools.getAppSession(sid);
			if(session!=null)
			{
			user = (user) session.getAttribute("User");
			role = (role) session.getAttribute("Role");
			}
		}
		
		if (user == null||role==null)
		{
			status = 403;
		}
		else if(StringUtils.isBlank("sosiid")){
			
			status=406;
		}
		else {
			//System.out.println(sosiid);
			
			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("sosiid",sosiid);
			sos = sosMapper.selectByPrimaryKeyWithAll(map1);
			
			if(sos==null)
				status=406;
			else {

		      if(sos.getRoleid()==role.getRoleid())
		     	status = 200;
		      else
		    	  status=406;
			}
		}

		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("status", "success");
			map.put("code", 200);
			map.put("result", sos);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {

			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "parameters error");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
}
